package com.gitee.aurora.server.dao.impl;

import com.gitee.aurora.core.comm.AuroraJdbcTemplate;
import com.gitee.aurora.server.dao.ReportDao;
import com.gitee.aurora.server.vo.PtSjzxVo;
import com.gitee.aurora.server.vo.ReportExport;
import com.gitee.aurora.server.vo.ReportVo;
import com.gitee.aurora.utils.PageParam;
import com.gitee.aurora.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;


import java.util.ArrayList;
import java.util.List;

@Repository
public class ReportDaoImpl implements ReportDao {

    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;

    @Override
    public Pagination queryReport(ReportVo reportVo, PageParam pageParam) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT ID AS id, ZFJDNUM AS zfjdNum, SJZXNUM AS sjzxNum, BAQNUM AS baqNum, SOURCEIP AS sourceIp, TABLENAME AS tableName, INSERTTIME AS insertTime, CASE WHEN ZFJDNUM <> SJZXNUM THEN '是' WHEN ZFJDNUM <> BAQNUM THEN '是' WHEN SJZXNUM <> BAQNUM THEN '是' ELSE '否' END AS abnormalFlag FROM LOG_RESULT WHERE 1=1");
        if (reportVo != null){
            if (reportVo.getAbnormalFlag() != null && "1".equals(reportVo.getAbnormalFlag())){
                sql.append(" and (ZFJDNUM <> SJZXNUM or SJZXNUM <> BAQNUM or BAQNUM <> ZFJDNUM)");
            }
            if (reportVo.getAbnormalFlag() != null && "0".equals(reportVo.getAbnormalFlag())){
                sql.append(" and (ZFJDNUM = SJZXNUM and SJZXNUM = BAQNUM and BAQNUM = ZFJDNUM)");
            }
            if (reportVo.getSourceIp()!= null && !"".equals(reportVo.getSourceIp())){
                sql.append(" and SOURCEIP = ?");
                params.add(reportVo.getSourceIp());
            }
            if (reportVo.getStartTime() != null && !"".equals(reportVo.getStartTime())){
                sql.append(" and INSERTTIME >= ?");
                params.add(reportVo.getStartTime());
            }
            if (reportVo.getEndTime() != null && !"".equals(reportVo.getEndTime())){
                sql.append(" and INSERTTIME <= ?");
                params.add(reportVo.getEndTime());
            }
        }
        return  new Pagination(sql.toString(),params.toArray(),pageParam,auroraJdbcTemplate);
    }

    @Override
    public List<PtSjzxVo> querySjzxList() {
        StringBuilder sql = new StringBuilder("SELECT T1.S_CODE as sCode, T1.S_CODE_NAME as sCodeName, T1.PT_IP as ptIp FROM TAB_PTCJ_CONFIG T1 LEFT JOIN TAB_PT_CONFIG T2 on T1.S_CODE = T2.S_CODE WHERE 1 = 1 AND T2.PT_CODE = '2' GROUP BY T1.S_CODE");
        return auroraJdbcTemplate.queryForMapToList(sql.toString(),PtSjzxVo.class);
    }

    @Override
    public List<ReportExport> reportExport(ReportVo reportVo) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT ID AS id, ZFJDNUM AS zfjdNum, SJZXNUM AS sjzxNum, BAQNUM AS baqNum, SOURCEIP AS sourceIp, TABLENAME AS tableName, INSERTTIME AS insertTime, CASE WHEN ZFJDNUM <> SJZXNUM THEN '是' WHEN ZFJDNUM <> BAQNUM THEN '是' WHEN SJZXNUM <> BAQNUM THEN '是' ELSE '否' END AS abnormalFlag FROM LOG_RESULT WHERE 1 =1");
        if (reportVo != null){
            if (reportVo.getAbnormalFlag() != null && "1".equals(reportVo.getAbnormalFlag())){
                sql.append(" and (ZFJDNUM <> SJZXNUM or SJZXNUM <> BAQNUM or BAQNUM <> ZFJDNUM)");
            }
            if (reportVo.getAbnormalFlag() != null && "0".equals(reportVo.getAbnormalFlag())){
                sql.append(" and (ZFJDNUM = SJZXNUM and SJZXNUM = BAQNUM and BAQNUM = ZFJDNUM)");
            }
            if (reportVo.getSourceIp()!= null && !"".equals(reportVo.getSourceIp())){
                sql.append(" and SOURCEIP = ?");
                params.add(reportVo.getSourceIp());
            }
            if (reportVo.getStartTime() != null && !"".equals(reportVo.getStartTime())){
                sql.append(" and INSERTTIME >= ?");
                params.add(reportVo.getStartTime());
            }
            if (reportVo.getEndTime() != null && !"".equals(reportVo.getEndTime())){
                sql.append(" and INSERTTIME <= ?");
                params.add(reportVo.getEndTime());
            }
        }
        return auroraJdbcTemplate.query(sql.toString(),new BeanPropertyRowMapper<>(ReportExport.class),params.toArray());
    }

}
